<template>
  <div>
    <van-search
      v-model="searchSubject"
      placeholder="输入科目名称"
      @search="searchScores"
      @clear="searchSubject = ''"
      @focus="showSearch = true"
      @blur="showSearch = false"
    />
    <van-dropdown-menu>
      <van-dropdown-item v-model="subjectValue" :options="subjectOptions" title="科目(下拉式菜单，内容可点击)" />
    </van-dropdown-menu>
    <div v-if="showSearch">
      <div class="search-result">
        <h2>搜索结果</h2>
        <ul>
          <li v-for="(scores, subject) in filteredSubjectScores" :key="subject">
            <p>科目: {{ subject }}</p>
            <ul>
              <li v-for="score in scores" :key="score.id">
                <p>姓名: 耀××</p>
                <p>成绩: {{ score.score }}</p>
                <p>评论: {{ score.comments }}</p>
              </li>
            </ul>
          </li>
        </ul>
      </div>
    </div>
    <div v-else>
      <h1 class="kk">个人成绩</h1>
      <div v-for="(scores, subject) in subjectScores" :key="subject">
        <h2>{{ subject }}</h2>
        <ul>
          <li v-if="scores.length > 0">
            <p>姓名: 耀××</p>
            <p>成绩: {{ scores[0].score }}</p>
            <p>评论: {{ scores[0].comments }}</p>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue';

// 下拉菜单的数据
const subjectValue = ref(null);
const subjectOptions = [
  
  { text: 'Java', value: 'Java' },
  { text: 'C++', value: 'C++' },
  { text: 'HTML', value: 'HTML' },
  { text: 'CSS', value: 'CSS' },
  { text: 'Vue3', value: 'Vue3' },
];

const subjects = ref(['Java', 'C++', 'HTML', 'CSS', 'Vue3']);
const subjectScores = ref({
  Java: [
    { id: 1, score: 99, comments: '优秀' },
  ],
  C: [
    { id: 2, score: 85, comments: '良好' },
  ],
  HTML: [
    { id: 3, score: 92, comments: '优秀' },
  ],
  CSS: [
    { id: 4, score: 88, comments: '良好' },
  ],
  Vue3: [
    { id: 5, score: 95, comments: '优秀' },
  ],
});

const searchSubject = ref('');
const showSearch = ref(false);
const filteredSubjectScores = ref({});

watch(subjectValue, (newSubjectValue) => {
  if (newSubjectValue) {
    searchScores(newSubjectValue);
  } else {
    showSearch.value = false;
  }
});

function searchScores(selectedSubject) {
  showSearch.value = true;
  filteredSubjectScores.value = {};
  const searchValue = selectedSubject; // 直接使用选中的科目值作为搜索关键词
  for (const subject in subjectScores.value) {
    if (subject === searchValue) {
      filteredSubjectScores.value
      filteredSubjectScores.value[subject] = subjectScores.value[subject];
}
}
}
</script>

<style>
body {
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
  background-color: #f7f7f7;
  margin: 0;
  padding: 0;
}

/* 搜索栏样式 */
.van-search {
  margin: 20px;
  border-radius: 5px;
}

/* 下拉菜单样式 */
.van-dropdown-menu {
  margin: 0 20px;
}

/* 结果列表样式 */
ul {
  list-style: none;
  padding: 0;
}

li {
  background-color: #fff;
  border-radius: 5px;
  padding: 15px;
  margin: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

li:hover {
  transform: translateY(-5px);
}

.taskText, .classText {
  font-weight: bold;
}

button {
  margin-left: 10px;
  padding: 5px 10px;
  border: none;
  border-radius: 5px;
  background-color: #007bff;
  color: white;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: #0056b3;
}

.feedback-form {
  margin-top: 10px;
}

textarea {
  width: 100%;
  padding: 10px;
  margin-top: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  box-sizing: border-box;
  resize: vertical;
}

/* 反馈计数器样式 */
.feedback-count {
  font-size: 0.8em;
  color: #888;
}

/* 没有结果时的样式 */
.nn {
  text-align: center;
  margin: 20px;
  font-size: 1.5em;
  color: #888;
}

/* 标题样式 */
h2 {
  font-size: 1.5em;
  text-align: center;
  margin-bottom: 20px;
  color: #333;
}

.search-result {
  margin-top: 10px;
}
.kk{
  font-size:20px
}
#van-search-16-input{
  width:300px;
  height:40px;
}
.van-dropdown-menu {
  margin: 20px;
}
</style>
